package com.openstack.javasdk;

import com.alibaba.fastjson.JSONObject;
import org.openstack4j.api.Builders;
import org.openstack4j.api.OSClient;
import org.openstack4j.model.compute.Flavor;
import org.openstack4j.model.identity.v3.Credential;
import org.openstack4j.model.identity.v3.Region;
import org.openstack4j.model.identity.v3.User;
import org.openstack4j.model.network.Network;
import org.openstack4j.model.image.Image;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.openstack4j.api.OSClient.OSClientV3;
import org.openstack4j.openstack.OSFactory;
import org.openstack4j.model.common.Identifier;

import java.util.List;
import org.openstack4j.model.identity.v3.Project;

@SpringBootApplication
@RestController
public class JavasdkApplication {

	public static void main(String[] args) {
		SpringApplication.run(JavasdkApplication.class, args);
	}

	@GetMapping("/hello")
	public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
		Identifier domainIdentifier = Identifier.byId("default");

// unscoped authentication
// as the username is not unique across domains you need to provide the domainIdentifier
		OSClientV3 os = OSFactory.builderV3()
				.endpoint("http://192.168.31.229:5000/v3")
				.credentials("admin","admin", domainIdentifier)
				.authenticate();

//		Project project = os.identity().projects().get("projectId");
//		List<? extends User> users = os.identity().users().list();
//		List<? extends Flavor> flavors = os.compute().flavors().list();
//		List<? extends User> users = os.identity().users().list();
//		System.out.println("users: "+users.size());
		List<? extends Flavor> flavors = os.compute().flavors().list();
//		name = os.compute().servers().getVNCConsole("fb272900-e536-4985-802d-dc3832d0fe6c", null).getURL();
		List<? extends Network> networks = os.networking().network().list();
		System.out.println(flavors);
		System.out.println("networks:");
		System.out.println(networks);


		Flavor flavor1 = os.compute().flavors()
				.create("f1", 4096, 2, 120, 200, 0, 1.2f, true);
		List<? extends Flavor> flavors2 = os.compute().flavors().list();
		System.out.println(flavors2);

		List<? extends Region> regionList = os.identity().regions().list();
		System.out.println(regionList);
//		List<? extends Project> projectList = os.identity().projects().list();
////		System.out.println(projectList);

		return String.format("Hello %s!", flavors2);
	}

	private  OSClientV3 auth(){
		Identifier domainIdentifier = Identifier.byId("default");
		OSClientV3 os = OSFactory.builderV3()
				.endpoint("http://192.168.31.229:5000/v3")
				.credentials("admin","admin", domainIdentifier)
				.authenticate();
		return os;
	}


// You are not authorized to perform the requested action: identity:create_user.
	@GetMapping("/createUser")
	public String createUser(@RequestParam(value = "name", defaultValue = "World") String name) {
		OSClientV3 os = auth();
		User user = os.identity().users().create(Builders.user()
				.name("Foobar")
				.description("A new user.")
				.password("secret")
				.email("foobar@example.org")
				.domainId("default")
				.build());
		return String.format("Hello %s!", user);
	}

	@GetMapping("/credentialList")
	public String credentialList(){
		OSClient.OSClientV3 os = auth();
		List<? extends Credential> credentialList = os.identity().credentials().list();
		String json = JSONObject.toJSONString(credentialList);
		return json;
	}

	@GetMapping("/createCredential")
	public String createCredential(){
		OSClientV3 os = auth();
		Credential credential = os.identity().credentials().create(Builders.credential()
				.blob("{\"access\":\"181931\",\"secret\":\"secretKey\"}")
				.type("ec2")
				.projectId("e0af27432aee4d74b88484c3779c3e42")
				.userId("dd28358d2df2486287d0da37cbee0fc6")
				.build());
		Object obj = JSONObject.toJSON(credential);
		String json = obj.toString();
		return json;
	}

	@GetMapping("/queryCredential")
	public String queryCredential(@RequestParam(value = "credentialId",required = true,defaultValue = "") String credentialId){
		OSClientV3 os = auth();
		Credential credential = os.identity().credentials().get(credentialId);
		String json = JSONObject.toJSONString(credential);
		return json;
	}

	@GetMapping("/updateCredential")
	public String updateCredential(){
		OSClientV3 os = auth();
		Credential credential = os.identity().credentials().get("ae36602e835789d66ecc78bf274fbbaa5832c54705221f7244566a6fdb9d7e83");
		if (credential != null)
			credential = os.identity().credentials().update(credential.toBuilder()
					.blob("{\"access\":\"281920\",\"secret\":\"updatedSecretKey\"}")
					.build());
		String json = JSONObject.toJSONString(credential);
		return json;
	}

	@GetMapping("/deleteCredential")
	public String deleteCredential(@RequestParam(value = "credentialId",defaultValue = "") String credentialId){
		OSClientV3 os = auth();
		String json = JSONObject.toJSONString(os.identity().credentials().delete(credentialId));
		return json;
	}
}

